import { getBrandList, getKindList, getProListbyBrand } from "../../api/kind";
// pages/kind/kind.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    categorylist: [],
    categorybrandlist: [],
    categorybrandprolist: [],
    activeKey: 0,
    activeIndex: 0,
    categoryName: "",
    brandName: "",
    count:1,
    limitNum:10
  },
  // 获取品牌
  getMyBrandListFn(category) {
    getBrandList(category).then((res) => {
      // console.log("品牌列表");
      // console.log(res)
      this.setData({
        categorybrandlist: res.data,
        brandName: res.data[0].brand,
        activeIndex: 0,
      });
    });
  },
  // 获取品牌下产品列表
  getMyProListbyBrandFn(data) {
    getProListbyBrand(data).then((res) => {
      console.log('产品列表');
      
      console.log(res)
      this.setData({
        categorybrandprolist: res,
      });
    });
  },
  //点击分类
  getBrClick(event) {
    // console.log(event);
    // console.log(event.currentTarget.dataset.id);
    this.setData({
      activeKey: event.currentTarget.dataset.id,
      categoryName: event.currentTarget.dataset.item,
    });
    const name = event.currentTarget.dataset.item;
    this.getMyBrandListFn(name);
    this.getMyProListbyBrandFn({
      count:this.data.count,limitNum:this.data.limitNum,category:this.data.categoryName,
      brand:this.data.brandName
    })
  },

  // 品牌切换
  onBrandChange(e) {
    console.log(e);
    this.setData({
      activeIndex: e.detail.index,
      brandName: e.detail.title,
    });
  },
  // 点击品牌获取产品列表
  getProList(event) {
    console.log(event);
    // this.getMyProListbyBrandFn(event.currentTarget.dataset.item)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getKindList().then((res) => {
      // console.log(res.data)
      this.setData({
        categorylist: res.data,
        categoryName: res.data[0],
      });
      this.getMyBrandListFn(res.data[0]);
      this.getMyProListbyBrandFn({
        count:this.data.count,limitNum:this.data.limitNum,category:this.data.categoryName,
        brand:this.data.brandName
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
